using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;

using Icodeon.Scorm2CC.Console;
using Icodeon.Scorm2CC.GUI;
using Icodeon.Util;

// Logging
//#if LOG4NET
using log4net;
using log4net.Config;
using log4net.Core;
//#endif

namespace Icodeon.Scorm2CC
{
    public class Program
    {
        /// <summary>
        /// Console application entry point.
        /// 
        /// /?      Displays a usage message to stdout.
        /// /about  Displays an info message to stdout.
        /// /help   Displays a usage message to stdout.
        /// /in:    Specifies the input path to directory containing SCORM packages.
        /// /out:   Specifies the output path to directory containing CC packages.
        /// /xsl:   Path to the XSLT file to be used, parameterized for SCORM version 1.2 and 2004
        /// /gui:   Use 'console' for console. Other tokens or none launches the GUI
        /// 
        /// Examples:
        /// 
        /// s2cc /in:scorm /xsl:scorm2cc/scorm${version}cc.xslt /out:cc /log:debug 
        /// 
        /// </summary>
        [STAThread]
        public static void Main(string[] args)
        {
            System.Console.WriteLine(args.Length);

            if (args.Length != 0)
            {
                foreach (string s in args)
                {
                    if (s.Equals("?"))
                    {
                        ConsoleSupport.PrintUsage();
                        return;
                    }
                    else if (s.Equals("help"))
                    {
                        ConsoleSupport.PrintUsage();
                        return;
                    }
                    else if (s.Equals("about"))
                    {
                        ConsoleSupport.PrintAbout();
                        return;
                    }
                    else if (s.Equals("quit"))
                    {
                        Environment.Exit(0);
                    }
                }

                Scorm2ccDefaults defaults = new Scorm2ccDefaults();
                ConsoleUtils consoleUtils = new ConsoleUtils(args);
                IOUtils ioUtils = new IOUtils();

                string inStr = consoleUtils["in"];
                string outStr = consoleUtils["out"];
                string xslStr = consoleUtils["xsl"];
                string uiStr = consoleUtils["ui"];

                // Input
                DirectoryInfo input = defaults.Input;
                if (!String.IsNullOrEmpty(inStr))
                {
                    input = new DirectoryInfo(inStr);
                }

                // Output
                DirectoryInfo output = defaults.Output;
                if (!String.IsNullOrEmpty(outStr))
                {
                    output = new DirectoryInfo(outStr);
                }

                // SCORM 1.2  & 2004 XSL
                FileInfo xslFileForScorm1p2 = defaults.XslFileForScorm1p2;
                FileInfo xslFileForScorm2004 = defaults.XslFileForScorm2004;
                if (!String.IsNullOrEmpty(xslStr))
                {
                    string scorm1p2FilePath = ioUtils.ReplaceParameter(xslStr, "1.2");
                    xslFileForScorm1p2 = new FileInfo(scorm1p2FilePath);
                    string scorm2004FilePath = ioUtils.ReplaceParameter(xslStr, "2004");
                    xslFileForScorm2004 = new FileInfo(scorm2004FilePath);
                }

                System.Console.WriteLine(uiStr);
                System.Console.WriteLine(uiStr + " " + uiStr.Equals(ConsoleSupport.UI_TYPE_CONSOLE));


                if (uiStr.Equals(ConsoleSupport.UI_TYPE_CONSOLE))
                {
                    // Console launch
                    RunConsoleUserInterface(input,
                        output,
                        xslFileForScorm1p2,
                        xslFileForScorm2004);
                }
                else
                {
                    // GUI launch
                    RunGraphicalUserInterface(input,
                        output,
                        xslFileForScorm1p2,
                        xslFileForScorm2004);
                }
            }
            else
            {
                RunGraphicalUserInterface();
            }
        }

        private static void RunConsoleUserInterface(
            DirectoryInfo input,
            DirectoryInfo output,
            FileInfo xslFileForScorm1p2,
            FileInfo xslFileForScorm2004)
        {
            MainConsole.Program(
                input, 
                output, 
                xslFileForScorm1p2, 
                xslFileForScorm2004);
        }

        private static void RunGraphicalUserInterface(
            DirectoryInfo input,
            DirectoryInfo output,
            FileInfo xslFileForScorm1p2,
            FileInfo xslFileForScorm2004)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainWindow(
                input,
                output,
                xslFileForScorm1p2,
                xslFileForScorm2004));
        }

        private static void RunGraphicalUserInterface()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainWindow());
        }
    }
}
